import { treeToString } from '@/utils/tree'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

export function useProductUtils() {
  /** 获取分类的节点的完整结构 */
  const cardDate = getIntDictOptions(DICT_TYPE.SPU_CARD_DATE_UNIT)
  const formatCategoryNameByCategoryList = (categoryId: number[], categoryList: any) => {
    if (!categoryId || !categoryList) {
      return '-'
    }
    if (categoryId && categoryId.length > 0) {
      let str = ''

      for (let index = 0; index < categoryId.length; index++) {
        str += treeToString(categoryList, categoryId[index]) + '  '
      }
      return str
    } else {
      return '-'
    }
  }
  /**
   * 处理卡项属性展示
   * @param item  商品对应的sku
   * @param isSowLabel 是否展示标签
   * @param isSowAttrValues  是否展示属性
   * @returns
   */
  const filterCardAttributes = (
    item: any,
    isSowLabel: any = false,
    isSowAttrValues: any = true
  ) => {
    const usableDurationUnit = cardDate.find((ele) => {
      return ele.value == item.usableDurationUnit
    })?.label

    // 如果是计次卡
    let label = ''
    let attrValues = ''
    if (item.skuType == 20) {
      label = `次数`
      attrValues = `${item.usableTimes}次`
    } else if (item.skuType == 21) {
      label = `时间`
      if (item.usableDurationUnit == 3) {
        attrValues = '不限时'
      } else {
        // 如果是计时卡
        attrValues = `${item.usableDuration}${usableDurationUnit}`
      }
    } else {
      // 如果是计时计次卡
      label = `时间/次数`

      if (item.usableDurationUnit == 3) {
        attrValues = `不限时/${item.usableTimes}次`
      } else {
        attrValues = `${item.usableDuration}${usableDurationUnit}/${item.usableTimes}次`
      }
    }
    return `${isSowLabel ? label : ''} ${isSowAttrValues ? attrValues : ''}`
  }
  const skuSpecFormat = (properties: any) => {
    let sku_spec_format = ''
    if (properties.length > 0) {
      sku_spec_format = properties
        .map((value: any) => {
          return value.valueName
        })
        .toString()
    } else {
      sku_spec_format = ''
    }
    return sku_spec_format
  }

  return {
    formatCategoryNameByCategoryList,
    filterCardAttributes,
    skuSpecFormat
  }
}
